package com.scalar.chiptrack.operations.dataentry.fab.actioncommands;

import com.scalar.chiptrack.actioncommands.ActionCommand;
import com.scalar.chiptrack.users.User;
import com.scalar.chiptrack.users.Functionality;
import com.scalar.chiptrack.operations.dataentry.fab.forms.FabDataEntryForm;
import com.scalar.chiptrack.operations.dataentry.dao.DataEntryDAO;
import com.scalar.chiptrack.operations.dataentry.LotDetails;
import com.scalar.chiptrack.operations.process.dao.PartNumber;
import com.scalar.chiptrack.commons.SessionKeys;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForm;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.ArrayList;

/**
 * Created by IntelliJ IDEA.
 * User: Sudhakar
 * Date: Sep 28, 2006
 * Time: 1:09:00 AM
 */
public class FabDataEntrySaveCommand extends ActionCommand
{

    private static final String ACTION_NAME = "/fabDataEntry.do";

    public ActionForward execute(ActionMapping mapping,
                                 ActionForm form,
                                 HttpServletRequest request,
                                 HttpServletResponse response) throws Exception
    {

        FabDataEntryForm  fabDataEntryForm = ( FabDataEntryForm ) form;

        HashMap errors = new HashMap();
        HashMap info = new HashMap();
        String action = request.getParameter("action");

        request.setAttribute(INFO_MESSAGES, info);
        request.setAttribute(ERROR_MESSAGES, errors);

        HttpSession mSession = request.getSession();
        User user = (User) mSession.getAttribute(SessionKeys.USER_KEY);

        if (user == null)
        {
            mSession.setAttribute(SessionKeys.SOURCE_PAGE_KEY, ACTION_NAME);
            return (mapping.findForward("login"));
        }
        else
        {
            if (user.isActionAllowed(Functionality.DATA_ENTRY, User.USER_ACTION_MODIFY))
            {
                request.setAttribute("showMessage", "FALSE");
            }
            else
            {
                request.setAttribute("showMessage", "TRUE");
            }
        }

        ArrayList fabWipList = new ArrayList();
        ArrayList partNumberList ;
        PartNumber partNumber;
        ArrayList fabPartNumbersLableValueBeans = new ArrayList();

        LotDetails fabLotDetails = fabDataEntryForm.getFabLotDetails();
        fabLotDetails.setProcessStepId( fabDataEntryForm.getProcessStepId() );
        fabLotDetails.setLocationId( fabDataEntryForm.getLocationId() );
        fabLotDetails.setLotId( fabDataEntryForm.getLotId() );
        fabLotDetails.setPoId( fabDataEntryForm.getPoId() );

        if( fabLotDetails.getWaferQtyOut().trim().length() <= 0 )
        {
            fabLotDetails.setWaferQtyOut(null);
        }
        if( fabLotDetails.getDateOut().trim().length() <= 0 )
        {
            fabLotDetails.setDateOut(null);
        }

        fabWipList.add( fabLotDetails );

        if( fabWipList != null && fabWipList.size() > 0 )
        {

            try
            {
                DataEntryDAO.saveDataEntryFabLot( null, fabWipList );
                info.put ( "Lot Information Saved", null );
                partNumberList = DataEntryDAO.getPartNumber(LOCATION_TYPE_FAB );

                for (int i = 0; i < partNumberList.size(); i++)
                {
                    partNumber = (PartNumber) partNumberList.get(i);
                    fabPartNumbersLableValueBeans.add(new org.apache.struts.util.LabelValueBean(partNumber.getPartNumber(), partNumber.getProcessStepId()));
                }
                request.setAttribute("partNumbersList", fabPartNumbersLableValueBeans);
                request.setAttribute("action", action);

            }
            catch( Exception e )
            {
                e.printStackTrace();
                 errors.put( "Error storing the Fab Lot information", null );
            }
        }

        fabDataEntryForm.setProcessStepId("0");
        fabDataEntryForm.setLocationId("0");
        fabDataEntryForm.setLotId("0");
        fabDataEntryForm.setPoId("0");
        fabDataEntryForm.setFablotDetails(null);

        return mapping.findForward( "fabDataEntry" );
    }
}
